<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicModal
      v-bind="$attrs"
      width="1000px"
      :wrapperFooterOffset="180"
      :centered="true"
      @register="registerModal"
      @ok="handleSubmit"
    >
      <template #title>
        <span> {{ getTitle.value }} </span>
      </template>
      <BasicTable @register="registerForm" @success="handleSuccess" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewSportsSportsWeightAdditionalModal',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { FormProps } from '/@/components/Form';
  //   import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import {
    SportsPhysicalSkills,
    sportsPhysicalSkillsListData,
  } from '/@/api/physical/sportsPhysicalSkills';
  const emit = defineEmits(['success', 'register']);
  const { t } = useI18n('sports.SportsPhysicalSkills');
  const record = ref<SportsPhysicalSkills>({} as SportsPhysicalSkills);
  const getTitle = computed(() => ({
    value: t('选择对应考核项目'),
  }));
  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('类型'),
        field: 'termType',
        component: 'Select',
        componentProps: {
          dictType: 's_events_type',
          allowClear: true,
        },
      },
    ],
  };
  const tableColumns: BasicColumn[] = [
    {
      title: t('项名称'),
      dataIndex: 'termName',
      key: 'a_term_name',
      width: 400,
      align: 'left',
    },
    {
      title: t('类型'),
      dataIndex: 'termType',
      key: 'a_term_type',
      width: 100,
      align: 'left',
      dictType: 's_events_type',
    },
    {
      title: t('适配年级'),
      dataIndex: 'adaptGrade',
      key: 'a_adapt_grade',
      align: 'left',
      dictType: 'vke_grade',
    },
  ];
  const adaptGrades = ref();
  const [
    registerForm,
    { reload, getSelectRows, clearSelectedRowKeys, getSelectRowKeys, setSelectedRowKeys },
  ] = useTable({
    api: sportsPhysicalSkillsListData,
    columns: tableColumns,
    beforeFetch: (params) => {
      console.log(params);
      params.adaptGrades = adaptGrades.value;
    },
    showIndexColumn: false,
    formConfig: searchForm,
    useSearchForm: true,
    clickToRowSelect: true,
    clearSelectOnPageChange: false,
    pagination: {
      showSizeChanger: false,
      pageSize: 10,
    },
    rowSelection: { type: 'checkbox' },
    canResize: true,
  });
  let type;
  let sportsPlanCheckArr;
  const [registerModal, { closeModal, setModalProps }] = useModalInner(async (data) => {
    reload();
    console.log(data);
    type = data.type;
    adaptGrades.value = data.adaptGrade;
    sportsPlanCheckArr = data.sportsPlanCheckArr;
    console.log(data.sportsPlanCheckId);
    setModalProps({ loading: true });
    setSelectedRowKeys(data.sportsPlanCheckId);
    setModalProps({ loading: false });
  });
  async function handleSubmit() {
    let data = getSelectRows();
    let arr = getSelectRowKeys();
    console.log(data);
    sportsPlanCheckArr.forEach((item) => {
      arr.forEach((arrItem) => {
        if (item.id == arrItem) {
          if (data.findIndex((dataItem) => dataItem.id == item.id) == -1) {
            console.log(item);
            data.push(item);
          } else {
            data[data.findIndex((dataItem) => dataItem.id == item.id)] = item;
          }
        }
      });
    });
    console.log(data, arr);
    emit('save', data, type);
    clearSelectedRowKeys();
    closeModal();
  }
  function onSelectChange(selectedRowKeys) {
    selectedRowKeys = selectedRowKeys;
  }
  function handleSuccess() {}
</script>
